﻿using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Base.Realization
{
    [Service(RegistName = "Local")]
    public class LocalFileAttachStroage : IAttachStorage
    {
        [AutoWired]
        private DB_AttachInfo DB_AttachInfo;
        public virtual void DeleteAttach(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = DB_AttachInfo.SelectOne(AttachGuid);
            string VirtualPath = CloudUtil.GetAttachVirtualPath() + "/";
            string AbsoulateVirtualPath = CloudUtil.GetContentPath() + "/" + VirtualPath + "/";
            string GroupTypePath = string.IsNullOrEmpty(AttachInfo.GroupType) ? string.Empty : AttachInfo.GroupType + "/";
            string AttachVirtualPath = VirtualPath + GroupTypePath + AttachInfo.AttachGuid + "/";
            string AbsoulateAttachVirtualPath = CloudUtil.GetContentPath() + "/" + AttachVirtualPath;

            string FilePath = AttachInfo.VirtualPath;
            string AbsoulateFilePath = CloudUtil.GetContentPath() + "/" + FilePath;
            if (File.Exists(AbsoulateFilePath))
            {
                File.Delete(AbsoulateFilePath);
            }
            if (Directory.Exists(AbsoulateAttachVirtualPath))
            {
                DirectoryInfo AttachVirtualDir = new DirectoryInfo(AbsoulateAttachVirtualPath);
                if (AttachVirtualDir.GetFiles().Length == 0)
                {
                    AttachVirtualDir.Delete();
                }
            }
          
        }

        public virtual string GetAttachUrl(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = DB_AttachInfo.SelectOne(AttachGuid);
            return CloudUtil.GetRootPath() + AttachInfo.VirtualPath;
        }

        public virtual void InsertAttach(Frame_AttachInfo AttachInfo, byte[] AttachContent)
        {
            string VirtualPath = CloudUtil.GetAttachVirtualPath() + "/";
            string AbsoulateVirtualPath = CloudUtil.GetContentPath() + "/" + VirtualPath + "/";
            DirectoryInfo VirtualDir = new DirectoryInfo(AbsoulateVirtualPath);
            if (!VirtualDir.Exists)
            {
                VirtualDir.Create();
            }

            string GroupTypePath = string.IsNullOrEmpty(AttachInfo.GroupType) ? string.Empty : AttachInfo.GroupType + "/";
            string AttachVirtualPath = VirtualPath + GroupTypePath + AttachInfo.AttachGuid + "/";
            string AbsoulateAttachVirtualPath = CloudUtil.GetContentPath() + "/" + AttachVirtualPath;
            DirectoryInfo AttachVirtualDir = new DirectoryInfo(AbsoulateAttachVirtualPath);
            if (!AttachVirtualDir.Exists)
            {
                AttachVirtualDir.Create();
            }

            string FilePath = AttachVirtualPath + AttachInfo.AttachFileName;
            string AbsoulateFilePath = CloudUtil.GetContentPath() + "/" + FilePath;
            FileUtil.SaveFile(AbsoulateFilePath, AttachContent);

            AttachInfo.VirtualPath = FilePath;
            DB_AttachInfo.UpdateByID(AttachInfo);
        }
    }
}
